package middleware

import (
	"gin-backend/model"
	"github.com/gin-gonic/gin"
	"time"
)

func OperationRecord() gin.HandlerFunc {
	return func(c *gin.Context) {
		var body []byte
		record := model.SysOperationRecord{
			Ip:     c.ClientIP(),
			Method: c.Request.Method,
			Path:   c.Request.URL.Path,
			Agent:  c.Request.UserAgent(),
			Body:   string(body),
		}
		now := time.Now()
		c.Next()
		latency := time.Now().Sub(now)
		record.Status = c.Writer.Status()
		record.Latency = latency
	}
}
